import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;              
import java.awt.event.*;       
import java.net.URL;
import java.io.*;
import java.lang.Object;
import java.util.Properties;
 
 
        //formatka wyboru formatu transmisji
class LanOrWanFrame extends JFrame{
		Properties wlasciwosci = PropertyLoader.load("conf/System.properties"); 		
        String NString = wlasciwosci.getProperty("NETWORK");
        JTextField zewIP;
        
                public LanOrWanFrame(){
                  
                setTitle("Okno wyboru sieci ");
                setResizable(false);
                setSize(200,200);
                setLocation(260,0);

                JLabel text = new JLabel("Wybierz siec dla transmisji :");
                JLabel ipLabel = new JLabel("adres IP : ");
                zewIP = new JTextField(15);
                zewIP.setText(NString);
                zewIP.setEnabled(false);
                ipLabel.setLabelFor(zewIP);
                NumericFilter filter = new NumericFilter(zewIP);
              
                final JCheckBox N1Box = new JCheckBox("Siec LAN");
                final JCheckBox N2Box = new JCheckBox("Internet");

                
                if (NString.equals("LAN")){
                    N1Box.setSelected(true);
                    }else{
                    	N2Box.setSelected(true);
                    }
                
                JButton networkButton = new JButton("ustaw");
                JButton cencelButton = new JButton("anuluj");
            
                N1Box.addActionListener(new ActionListener(){
                		public void actionPerformed(ActionEvent e){
                			if (N1Box.isSelected()){
                				zewIP.setEnabled(false);
                				N2Box.setSelected(false);
                			}
                		}			
                });
                
                
                
                N2Box.addActionListener(new ActionListener(){
                	public void actionPerformed(ActionEvent e){
                       
                        if (N2Box.isSelected()){
                        	zewIP.setEnabled(true);
                        	N1Box.setSelected(false);
                    	}
                	}
                });
                
                networkButton.addActionListener(new ActionListener(){

					public void actionPerformed(ActionEvent e) {
						
						if(N1Box.isSelected()){
							try{
                				wlasciwosci.setProperty("NETWORK","LAN");
                				FileOutputStream os = new FileOutputStream("conf/System.properties");
                				wlasciwosci.store(os,"Zmiana ustawien");
                				dispose();
                			}catch(FileNotFoundException fnfe){}
                				catch(IOException ioe){}
                        }
						if(N2Box.isSelected()){
							
							try{
								String ipWan = zewIP.getText();
								wlasciwosci.setProperty("NETWORK",ipWan);
								Komunikator.caly_adres=ipWan;
								FileOutputStream os = new FileOutputStream("conf/System.properties");
								wlasciwosci.store(os,"Zmiana ustawien");
								dispose();
	                        	}catch(FileNotFoundException fnfe){}
	                        		catch(IOException ioe){}
							}
                        
						if(N1Box.isSelected()==false && N2Box.isSelected()==false ){
							JOptionPane.showMessageDialog(null,"Musisz wybrać sieć");
						}
						}
                });
                
                cencelButton.addActionListener(new ActionListener(){

					public void actionPerformed(ActionEvent e) {
						dispose();
						
					}
                	
                });
                
                N2Box.isValid();      
                JPanel panel = new JPanel();
                panel.setLayout(new GridLayout(5,1));
                panel.add(text);
                panel.add(N1Box);
                panel.add(N2Box);
                panel.add(ipLabel);
                panel.add(zewIP);
                JPanel buttonPanel = new JPanel();
                buttonPanel.add(networkButton, BorderLayout.LINE_END);
                buttonPanel.add(cencelButton, BorderLayout.AFTER_LAST_LINE);
                getContentPane().add(panel, BorderLayout.CENTER);
                getContentPane().add(buttonPanel, BorderLayout.SOUTH);
                

                }
	}
